package com.example.blemulticondemo;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

import com.example.blemulticondemo.adapter.BleDeviceAdapter;

/**
 * @author linvisf
 * @date 2024/8/6.
 * description：
 */
public class BleListDialog extends Dialog {
    private Context context;
    private RecyclerView lvBluetooth;
    private BleDeviceAdapter adapter;

    public BleListDialog(Context context) {
        super(context, R.style.Mydialog);
        this.context = context;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        View view = LayoutInflater.from(getContext()).inflate(R.layout.dialog_ble_list, null);
        Window window = getWindow();
        window.setContentView(view);

        lvBluetooth = findViewById(R.id.rvBluetooth);
        findViewById(R.id.tvClose).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });

        WindowManager.LayoutParams lp = getWindow().getAttributes();

        // 设置宽度充满屏幕
        lp.width = ScreenUtil.getScreenWidth(getContext());
        lp.height = (int) (ScreenUtil.getScreenHeight(getContext()) * 0.5);

        window.setAttributes(lp);

        window.setGravity(Gravity.BOTTOM);
//        window.setWindowAnimations(R.style.dialog_anim_style);

        setCancelable(false);
        setCanceledOnTouchOutside(false);

        initAdapter();
    }

    private void initAdapter() {
        BleManager bleManager = BleManager.getInstance(context);
        adapter = new BleDeviceAdapter(bleManager.getScannedDevices(), bleManager);
        lvBluetooth.setAdapter(adapter);
    }

    public void refreshDeviceList() {
        if (adapter != null) {
            adapter.notifyDataSetChanged();
        }
    }
}
